function dosearchhistoribayar(xAwal) {
    xSearch = "";
    try
    {
        if ($("#edSearch").val() != "") {
            xSearch = $("#edSearch").val();
        }
    } catch (err) {
        xSearch = "";
    }
    if (typeof (xSearch) == "undefined") {
        xSearch = "";
    }
    $(document).ready(function() {
        $.ajax({
            url: getBaseURL() + "index.php/ctrlaporanpembayaran/searchhistoribayar/",
            data: "xAwal=" + xAwal + "&xSearch=" + xSearch + '&date_mulai='+$("#eddate_mulai").val()+'&date_akhir='+$("#eddate_akhir").val(),
            cache: false,
            dataType: 'json',
            type: 'POST',
            success: function(json) {
                $("#tabledatahistoribayar").html(json.tabledatahistoribayar);
            },
            error: function(xmlHttpRequest, textStatus, errorThrown) {
                alert("Error juga" + xmlHttpRequest.responseText);
            }
        });
    });
}

function doedithistoribayar(edidx) {
    $(document).ready(function() {
        $.ajax({
            url: getBaseURL() + "index.php/ctrlaporanpembayaran/editrechistoribayar/",
            data: "edidx=" + edidx,
            cache: false,
            dataType: 'json',
            type: 'POST',
            success: function(json) {
                $("#edidx").val(json.idx);
                $("#edidcustomer").val(json.idcustomer);
                $("#edidpaket").val(json.idpaket);
                $("#edjenisbayar").val(json.jenisbayar);
                $("#edtotal").val(json.total);
                $("#eddate_bayar").val(json.date_bayar);
                $("#edidpegawai").val(json.idpegawai);
            },
            error: function(xmlHttpRequest, textStatus, errorThrown) {
                alert("Error juga " + xmlHttpRequest.responseText);
            }
        });
    });
}

function doClearhistoribayar() {
    $(document).ready(function() {
        $("#edidx").val("0");
        $("#edidcustomer").val("");
        $("#edidpaket").val("");
        $("#edjenisbayar").val("");
        $("#edtotal").val("");
        $("#eddate_bayar").val("");
        $("#edidpegawai").val("");
    });
}

function dosimpanhistoribayar(idcus) {
    $(document).ready(function() {
        $.ajax({
            url: getBaseURL() + "index.php/ctrlaporanpembayaran/simpanhistoribayar/",
            data: "edidx=" + $("#edidx").val() + "&edidcustomer=" + idcus + 
                    "&edidpaket=" + $("#edidpaket_"+idcus).val() +
                    "&edjenisbayar=" + $("#edjenisbayar_"+idcus).val() + 
                    "&edtotal=" + $("#edtotal_"+idcus).val() + "&eddate_bayar=" + $("#eddate_bayar_"+idcus).val() + 
                    "&edidpegawai=" + $("#edidpegawai").val(),
            cache: false,
            dataType: 'json',
            type: 'POST',
            success: function(msg) {
                doClearhistoribayar();
                dosearchhistoribayar('-99');
                alert("Data Berhasil Disimpan.... ");
            },
            error: function(xmlHttpRequest, textStatus, errorThrown) {
                alert("Error juga " + xmlHttpRequest.responseText);
            }
        });
    });
}
//$("#eddate_mulai").change(function() {
//    alert('&date_mulai='+$("#eddate_mulai").val()+'&date_akhir='+$("#eddate_akhir").val());
//});
//$("#eddate_akhir").change(function() {
//    dosearchhistoribayar();
//});
function dohapushistoribayar(edidx, edidcustomer) {
    if (confirm("Anda yakin Akan menghapus data " + edidcustomer + "?"))
    {
        $(document).ready(function() {
            $.ajax({
                url: getBaseURL() + "index.php/ctrlaporanpembayaran/deletetablehistoribayar/",
                data: "edidx=" + edidx,
                cache: false,
                dataType: 'json',
                type: 'POST',
                success: function(json) {
                    doClearhistoribayar();
                    dosearchhistoribayar('-99');
                },
                error: function(xmlHttpRequest, textStatus, errorThrown) {
                    alert("Error juga " + xmlHttpRequest.responseText);
                }
            });
        });
    }
}


dosearchhistoribayar(0);

function strpad(val) {
    return (!isNaN(val) && val.toString().length == 1) ? "0" + val : val;
}

function settanggal() {

    $(document).ready(function() {


        var currentTimeAndDate = new Date();
        var Date30 = new Date();
        var date = new Date();
        Date30.setDate(Date30.getDate() - 30);



        var dd = date.getDate();
        var mm = date.getMonth();
        var yy = date.getYear();

        var hh = date.getHours();
        var mnt = date.getMinutes();

        var dd30 = Date30.getDate();
        var mm30 = Date30.getMonth();
        var yy30 = Date30.getYear();

        yy = (yy < 1000) ? yy + 1900 : yy;
        yy30 = (yy30 < 1000) ? yy30 + 1900 : yy30;



//        $(".tanggal").datepicker({
//            //dateFormat: 'yy-mm-dd'
//            dateFormat: 'dd-mm-yy'
//        });
//
//        $(".tanggal").val(strpad(dd) + "-" + strpad(mm + 1) + "-" + yy);
        $("#eddate_mulai").datepicker({
            //dateFormat: 'yy-mm-dd'
            dateFormat: 'dd-mm-yy',
            numberOfMonths: 3,
      onClose: function() {
          dosearchcustomer(0);
      }
        });

        $("#eddate_mulai").val(strpad(dd) + "-" + strpad(mm + 1) + "-" + yy);
        $("#eddate_akhir").datepicker({
            //dateFormat: 'yy-mm-dd'
            dateFormat: 'dd-mm-yy',
            numberOfMonths: 3,
      onClose: function() {
          dosearchcustomer(0);
      }
        });

        $("#eddate_akhir").val(strpad(dd) + "-" + strpad(mm + 1) + "-" + yy);
       

    });
}

settanggal();

function dosearchcustomer(xAwal) {
    xSearch = "";
    try
    {
        if ($("#edSearch").val() != "") {
            xSearch = $("#edSearch").val();
        }
    } catch (err) {
        xSearch = "";
    }
    if (typeof (xSearch) == "undefined") {
        xSearch = "";
    }
    $(document).ready(function() {
        $.ajax({
            url: getBaseURL() + "index.php/ctrlaporanpembayaran/searchcustomer/",
            data: "xAwal=" + xAwal + "&xSearch=" + xSearch+ '&date_mulai='+$("#eddate_mulai").val()+'&date_akhir='+$("#eddate_akhir").val()+'&xstatus='+$("#edstatus").val(),
            cache: false,
            dataType: 'json',
            type: 'POST',
            success: function(json) {
                $("#tabledatacustomer").html(json.tabledatacustomer);
            },
            error: function(xmlHttpRequest, textStatus, errorThrown) {
                alert("Error juga" + xmlHttpRequest.responseText);
            }
        });
    });
}

function dosethargapaket(xidcus){
      $(document).ready(function() {
            $.ajax({
                url: getBaseURL() + "index.php/ctrlaporanpembayaran/dosethargapaket/",
                data: "edidpaket=" + $("#edidpaket_"+xidcus).val(),
                cache: false,
                dataType: 'json',
                type: 'POST',
                success: function(json) {
                  $("#edtotal_"+xidcus).val(json.hrgpaket)  
                },
                error: function(xmlHttpRequest, textStatus, errorThrown) {
                    alert("Error juga " + xmlHttpRequest.responseText);
                }
            });
        });  
}